import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home',
      meta: { title: 'Home' }
    },
    { path: '/home', component: () => import('@/views/home/HomePage.vue'), meta: { title: 'Home' } },
    {
      path: '/layout',
      component: () => import('@/views/layout/Layout.vue'),
      meta: { title: 'Data Structures' },
      children: [
        { path: '/layout/array', component: () => import('@/views/children/Array.vue'), meta: { title: 'Array' } },
        { path: '/layout/linkedlist', component: () => import('@/views/children/LinkedList.vue'), meta: { title: 'Linked List' } },
        {
          path: '/layout/sorting',
          component: () => import('@/views/children/Sorting.vue'),
          meta: { title: 'Sorting' },
          redirect: '/layout/sorting/bubble',
          children: [
            { path: '/layout/sorting/bubble', component: () => import('@/views/sort/Bubble.vue') },
            { path: '/layout/sorting/insert', component: () => import('@/views/sort/Insert.vue') },
            { path: '/layout/sorting/exam', component: () => import('@/views/sort/Exam.vue') },
          ]
        },
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

export default router
